#coding:utf-8
from google.appengine.ext import db
import datetime
import re

#验证数据
def datavalid(data='', datatype='author'):
    type_list = ['author', 'email', 'content', 'url', 'num', 'articleurl', 'tag', 'title', 'brief']
    if not datatype in type_list:
        return False
    #验证网址，email，昵称的正则表达式
    re_list = {
        'email': r'^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9]{2,3}$', 
        'url': r'^(http://|https://)?([a-zA-Z0-9_\-]+.){1,3}[a-zA-Z]{2,6}(/[a-zA-Z0-9_\.\-]+)?$',
        'author':r'^[^<>\/\"\;\:\\\|\@\#\+\=\,]+$',
        'tag':r'^[^<>\/\"\;\:\\\|\@\#\+\=]+$',
        'num': r'^[0-9]+$',
        'articleurl': r'^[a-zA-Z0-9\-\.\_]+$',
        'title': r'^[^<>]+$',
        #'brief': r'^[^<>]+$',
    }

    cp_list = {
        'email': re.compile(re_list['email']),
        'url': re.compile(re_list['url']),
        'author': re.compile(re_list['author']),
        'num': re.compile(re_list['num']),
        'articleurl': re.compile(re_list['articleurl']),
        'tag': re.compile(re_list['tag']),
        'title': re.compile(re_list['title']),
      #  'brief': re.compile(re_list['brief']),
    }
    #处理html标签，去掉两端空格，处理编码
    if data:
        data = data.strip().replace('<', '&lt;').replace('>', '&gt;').decode('UTF-8')
        if datatype == 'brief':
            if data == '':
                return data
        if datatype == 'url':
            if data == '' or data == 'http://' or data == 'None':
                data = '/'
                return data
        if datatype == 'content':
            if data:
                return data
            else:
                return False
        if datatype == 'articleurl':
            if not data:
                return datetime.datetime.now().strftime('%H%M%S')
        if datatype == 'tag':
            if not data:
                return [' ']
        if data:
            if cp_list[datatype].match(data):
                return data
            else:
                return False
    else:
        if datatype == 'articleurl':
            return datetime.datetime.now().strftime('%H%M%S')
        else:
            return False
    


#以下是类的定义
class BaseModel(db.Model):
    author = db.StringProperty(required=True, default=u'匆匆过客')
    time = db.DateTimeProperty(required=True, default=datetime.datetime.now())
    content = db.TextProperty(required=True, default=u'他很懒，什么也没留下')
    avatarurl = db.StringProperty(required=True, default=u'/templates/image/defaultavatar.jpg')

    def save(self):
        self.put()

class GuestBookModel(BaseModel):
    email = db.EmailProperty(required=True, default='abc@abc.com')
    webpage = db.StringProperty(required=False)
    
    def save(self):
        self.put()
    
class WebSiteModel(db.Model):
    title = db.StringProperty(required=True, default=u'SimpHome')
    subtitle = db.StringProperty(required=True, default=u'A Simple Personal Homepage')
    now = db.StringProperty(required=True, default=u'主页')
    home = db.StringProperty(required=True, default=u'/')
    timeoffset = db.IntegerProperty(required=True, default=8)
    numofentry = db.IntegerProperty(required=True, default=10)
    numofgreeting = db.IntegerProperty(required=True, default=10)
    numofcomment = db.IntegerProperty(required=True, default=20)
    orderofcomment = db.IntegerProperty(required=True, default=0)
    owner = db.StringProperty(required=True, default=u'Syndim')
    theme = db.StringProperty(required=True, default=u'default')
    
    def save(self):
        self.put()
   
class BannerModel(db.Model):
    name = db.StringProperty(required=True, default=u'主页')
    url = db.StringProperty(required=True, default=u'/')
    
    def save(self):
        self.put()

class BlogModel(BaseModel):
    title = db.StringProperty(required=True, default=u'无标题')
    readtimes = db.IntegerProperty(required=True, default=0)
    comments = db.IntegerProperty(required=True, default=0)
    url = db.StringProperty(required=True, default=datetime.datetime.now().strftime('%H%M%S'))
    category = db.StringProperty(required=True, default=u'未分类')
    tag = db.StringListProperty(required=True, default=[' '])
    briefview = db.TextProperty(required=False)
    posted = db.BooleanProperty(required=True, default=False)
    year = db.IntegerProperty(required=True, default=1900)
    month = db.IntegerProperty(required=True, default=01)
    day = db.IntegerProperty(required=True, default=01)
    
    def save(self):
        self.put()

class TagModel(db.Model):
    tag = db.StringProperty(required=True, default=' ')
    num = db.IntegerProperty(required=True, default=1)
    
    def save(self):
        self.put()
 
class CategoryModel(db.Model):
    category = db.StringProperty(required=True, default='未分类')
    num = db.IntegerProperty(required=True, default=0)
    
    def save(self):
        self.put()
        
class CommentModel(GuestBookModel):
    dairyid =  db.IntegerProperty(required=True, default=0)
    
    def save(self):
        self.put()
        